<?php

namespace app\index\model;

use think\Model;

class Article extends Model
{
    //查看所有文章
    public function sel() {
        $res = self::all();
        return $res;
    }
    //添加数据
    public function  add($data) {
        $res = self::create($data);
        if($res) {
            return $res;
        }else {
            return false;
        }
    }
    //删除文章
    public function del($id) {
        $res = self::get($id);
        $res = $res->delete();
        return $res;
    }

    /**
     * @api操作
     */

    //查看排行
    public function downApi() {
        $art = new self();
        $res = $art->field('id,title')->order('zan','desc')->limit(5)->select();
        if($res) {
            return collection($res)->toArray();
        }

    }
    //根据分类查询文章内容
    public function selApi($class_id) {
        $art = (new self())->where('class_id',$class_id)->field('id,title,content,zan,create_time')->select();
        if($art) {
            return collection($art)->toArray();
        }
    }
    //查询所有文章内容
    public function selAllApi() {
        $res = self::all(function($query) {
            $query->field('id,title,content,create_time,zan');
        });
        return $res;
    }

    //查询指定的文章
    public function selOneApi($id) {
        $res = (new self)->where('id',$id)->field('id,title,content')->select();
        if($res) {
            return collection($res)->toArray();
        }
    }
}
